﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IBatisNet.Common;
using IBatisNet.DataMapper;


namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //SimpleSelect();
                //SelectWithCondition();
                //SimpleInsert();
                InsertWithProc();
                //SimpleDelete();
                //SimpleUpdate();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine();
        }
        /// <summary>
        /// 简单查询
        /// </summary>
        static void SimpleSelect()
        {
            IList users = Mapper.Instance().QueryForList("getAllUsers", null);

            Console.WriteLine("Selected " + users.Count + " records.");
            string outputFormat = "UserName={1}, Password={2}, UserId={0}, GroupName={3}";
            for (int i = 0; i < users.Count; i++)
            {
                Modules.Account user = users[i] as Modules.Account;
                Console.WriteLine(string.Format(outputFormat, user.USERID, user.USERNAME, user.PASSSWORD, user.GROUPNAME));
            }
        }
        /// <summary>
        /// 带条件查询
        /// </summary>
        static void SelectWithCondition()
        {
            string groupName = "EMPLOYEE";
            IList users = Mapper.Instance().QueryForList("getUsersByGroupName", groupName);

            Console.WriteLine("Selected " + users.Count + " records.");
            string outputFormat = "UserName={1}, Password={2}, UserId={0}, GroupName={3}";
            for (int i = 0; i < users.Count; i++)
            {
                Modules.Account user = users[i] as Modules.Account;
                Console.WriteLine(string.Format(outputFormat, user.USERID, user.USERNAME, user.PASSSWORD, user.GROUPNAME));
            }
        }
        /// <summary>
        /// 简单插入
        /// </summary>
        static void SimpleInsert()
        {
            Console.WriteLine("Before Insert:");
            SimpleSelect();

            Modules.Account account = new ConsoleApplication.Modules.Account() { USERID = 0, USERNAME = "Raogs", PASSSWORD = "888", GROUPNAME = "EMPLOYEE_" };
            int ret = (int)Mapper.Instance().Insert("Insert-byclass", account);
            Console.WriteLine(System.Environment.NewLine + "Insert Identity :{0}" + System.Environment.NewLine, ret);

            Console.WriteLine("Insert Complete:");
            SimpleSelect();
        }
        /// <summary>
        /// 
        /// </summary>
        static void InsertWithProc()
        {
            Console.WriteLine("Before Insert by Proc:");
            SimpleSelect();

            Modules.Account account = new ConsoleApplication.Modules.Account() { USERID = 0, USERNAME = "Proc-Raogs", PASSSWORD = "7777", GROUPNAME = "EMPLOYEE_" };
            object ret=Mapper.Instance().QueryForObject("proc-insert", account);
            Console.WriteLine(System.Environment.NewLine + "Insert Identity :{0}" + System.Environment.NewLine, ret);

            Console.WriteLine("Insert by Proc Complete:");
            SimpleSelect();
        }

        static void SimpleDelete()
        {
            Console.WriteLine("Before Delete:");
            SimpleSelect();

            string username = "Raogs";
            int ret = Mapper.Instance().Delete("delete-byid", username);
            Console.WriteLine(System.Environment.NewLine + "delete Influence line number  :{0}" + System.Environment.NewLine, ret);

            Console.WriteLine("Delete Complete:");
            SimpleSelect();
        }

        static void SimpleUpdate()
        {
            Console.WriteLine("Before Update:");
            SimpleSelect();

            Modules.Account account = new ConsoleApplication.Modules.Account() { USERID = 3, USERNAME = "Proc-Raogs", PASSSWORD = "7777", GROUPNAME = "EMPLOYEE_" };
            int ret = Mapper.Instance().Update("update-byid", account);
            Console.WriteLine(System.Environment.NewLine + "update Influence line number  :{0}" + System.Environment.NewLine, ret);

            Console.WriteLine("update Complete:");
            SimpleSelect();
        }
    }
}
